<?php

namespace app\admin\model\ques;

use think\Model;
use think\Cache;
class School extends Model
{
    // 表名
    protected $name = 'ques_volume_school';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'publish_text',
        'z_publish_text',
        'book_text',
        'school_text',
    ];
    public function getPublishTextAttr($value, $data)
    {
        $value = isset($data['publish'])  ? $data['publish'] : '';
         if(!$userlist = Cache::get('USERLIST'))
        {
            $userlist = collection(model(GetTableModel('Admin'))::field("userid,username,gender,isleader,department,delete_status")->select())->toArray();
            $userlist = array_column($userlist,null,'userid');
            Cache::set('USERLIST',$userlist,604800);//有效期一个星期
        }
        return isset($userlist[$value]) ? $userlist[$value]['username']:$value.' 已离职';
    }
    public function getSchoolTextAttr($value, $data)
    {
        $value = isset($data['school_id']) ? $data['school_id'] : '';
        return $value ? TableField('School_B','name',$value):'';
    } 
    public function getBookTextAttr($value, $data)
    {
        $value = isset($data['book_id']) ? $data['book_id'] : '';
        return $value ? GetExplode(explode(',',$value),'Book'):'';
    }

    public function getZPublishTextAttr($value, $data)
    {
      $value = isset($data['z_publish'])  ? $data['z_publish'] : '';
         if(!$userlist = Cache::get('USERLIST'))
        {
             $userlist = collection(model(GetTableModel('Admin'))::field("userid,username,gender,isleader,department,delete_status")->select())->toArray();
            $userlist = array_column($userlist,null,'userid');
            Cache::set('USERLIST',$userlist,604800);//有效期一个星期
        }
        return isset($userlist[$value]) ? $userlist[$value]['username']:$value.' 已离职';
    }
    protected function setZPublishAttr($value)
    {
        return \think\Session::get('admin')['userid'];
    } 
    protected function setPublishAttr($value)
    {
        return \think\Session::get('admin')['userid'];
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    







}
